﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ParboiledSharp.lang.roboline.utils
{
    class StringUtils
    {
        public const char QUOTESYMBOL = '\"';

        public static bool IsQuoted(string str) {
            if (string.IsNullOrEmpty(str)|| str.Length<=1) return false;
            if (str[0] == QUOTESYMBOL || str[str.Length - 1] == QUOTESYMBOL) return true;
            return false;
        }

        public static string StripQuotes(string str)
        {
            if (!IsQuoted(str)) {
                return str;
            }
            return str.Substring(1,str.Length-2);
        }

        public static double Str2Dbl(string str){
            return Convert.ToDouble(str, CultureInfo.InvariantCulture);
        }

        public static string Quote(string str)
        {
            if (IsQuoted(str))
            {
                return str;
            }
            return QUOTESYMBOL + str + QUOTESYMBOL;
        }

        public static string unescapeString(string s) {
            if (!s.Contains('\\')) return s;
            string escapedString=s.Replace("\\n","\n").Replace("\\t","\t").Replace("\\r","\r");
            return escapedString;
        }

        public static string removeCharactersFromEnd(string str, int count) { 
            if(string.IsNullOrEmpty(str))return "";
            if (count >= str.Length) return "";
            return str.Substring(0, str.Length - count);
        }

        public static string removeCharactersFromStart(string str, int count)
        {
            if (string.IsNullOrEmpty(str)) return "";
            if (count >= str.Length) return "";
            return str.Substring(count, str.Length - count);
        }

        /// <summary>
        /// If the string str ends with  strToDelete -> removes it
        /// </summary>
        public static string removeStringFromEnd(string str, string strToDelete)
        {
            if (string.IsNullOrEmpty(str)) return "";
            if (string.IsNullOrEmpty(strToDelete)) return str;
            if (str.EndsWith(strToDelete)) { 
                return str.Substring(0,str.Length-strToDelete.Length);
            }
            return str;
        }

        public static string repeatString(string str, int count) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++) {
                sb.Append(str);
            }
            return sb.ToString();
        }
    }
}
